package io.renren.system.job.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.system.job.dao.ScheduleJobLogDao;
import io.renren.system.job.dto.ScheduleJobLogDTO;
import io.renren.system.job.entity.ScheduleJobLogEntity;
import io.renren.system.job.service.ScheduleJobLogService;
import io.renren.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 【系统设置-定时任务-定时任务日志】
 */
@Service
public class ScheduleJobLogServiceImpl extends BaseServiceImpl<ScheduleJobLogDao, ScheduleJobLogEntity> implements ScheduleJobLogService {

    private QueryWrapper<ScheduleJobLogEntity> getWrapper(Map<String, Object> params) {
        String jobId = StringUtils.checkObj2Str(params.get("jobId"));
        String beanName = StringUtils.checkObj2Str(params.get("beanName"));
        QueryWrapper<ScheduleJobLogEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(jobId), "job_id", jobId);
        wrapper.like(StringUtils.isNotEmpty(beanName), "bean_name", beanName);
        return wrapper;
    }

    /**
     * 根据查询条件分页获取定时任务日志列表
     *
     * @param params
     * @return
     */
    @Override
    public PageData<ScheduleJobLogDTO> page(Map<String, Object> params) {
        IPage<ScheduleJobLogEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false), getWrapper(params)
        );
        return getPageData(page, ScheduleJobLogDTO.class);
    }

    /**
     * 根据主键获取日志详细信息
     *
     * @param id
     * @return
     */
    @Override
    public ScheduleJobLogDTO get(Long id) {
        ScheduleJobLogEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, ScheduleJobLogDTO.class);
    }
}